//
//  MenuSearchController.m
//  吃货·上海
//
//  Created by lanou on 15/12/1.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "MenuSearchController.h"
#import "SHMenuModel.h"
#import "MBProgressHUD.h"
#import <BmobSDK/Bmob.h>
#import "DetailController.h"

@interface MenuSearchController () <UISearchResultsUpdating, UISearchBarDelegate>

@property(nonatomic,strong)UISearchController *searchVc;


@property(strong,nonatomic)NSMutableArray *items;

@property(strong,nonatomic)NSMutableArray *searchResults;

@end

@implementation MenuSearchController
{
    BOOL _isShow;
}


- (NSMutableArray *)items
{
    if (_items == nil) {
        self.items = [NSMutableArray array];
    }
    return _items;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    _isShow = NO;
    //获取数据
    [self getDataFromMyDB];
    self.navigationItem.title = @"快速查询";

    self.searchVc = [[UISearchController alloc]initWithSearchResultsController:nil];
    
    self.searchVc.searchResultsUpdater = self;
    self.searchVc.dimsBackgroundDuringPresentation = false;

    [self.searchVc.searchBar sizeToFit];
    
    self.searchVc.searchBar.delegate = self;
    self.searchVc.searchResultsUpdater = self;

    self.tableView.tableHeaderView = self.searchVc.searchBar;
}

#pragma mark - 数据请求
- (void)getDataFromMyDB
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    BmobQuery *bqeury = [BmobQuery queryWithClassName:@"Menu"];
    [bqeury findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            SHMenuModel *model =[[SHMenuModel alloc]init];
            
            model.title = [obj objectForKey:@"Dishname"];
            BmobFile *file = [obj objectForKey:@"icon"];
            model.imageUrl = file.url;
            model.info = [obj objectForKey:@"ifno"];
            model.myClass = [obj objectForKey:@"class"];
            model.mainmatcrials = [obj objectForKey:@"mainmatcrials"];
            model.makingMethod = [obj objectForKey:@"method"];
            model.makingmaterial = [obj objectForKey:@"material"];
            
            [self.items addObject:model];
            
        }
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [self.searchResults removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [CD] %@",self.searchVc.searchBar.text];
    self.searchResults = [[self.items filteredArrayUsingPredicate:predicate] mutableCopy];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
    _isShow = YES;
}


#pragma mark - UITabelView 代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _isShow ? self.items.count : self.searchResults.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    SHMenuModel *model = _isShow ? self.items[indexPath.row] : self.searchResults[indexPath.row];
    cell.textLabel.text = model.title;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailController *detailVc = [[DetailController alloc]init];
    
    detailVc.model = _isShow ? self.searchResults[indexPath.row] : self.items[indexPath.row];
    
    [self.navigationController pushViewController:detailVc animated:NO];
    NSLog(@"dianjile le ");
}




@end
